package net.ojbk.study.curriculumDesign.web.handlers.impl.canvasmodel;

import net.ojbk.study.curriculumDesign.beans.APIResult;
import net.ojbk.study.curriculumDesign.beans.canvasmodel.Canvas;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.common.api.APICodeConstant;
import net.ojbk.study.curriculumDesign.service.impl.canvasmodel.CanvasServiceImpl;
import net.ojbk.study.curriculumDesign.web.handlers.canvasmodel.ICanvasController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Pattern;

@Controller
@RequestMapping("canvas")
public class CanvasControllerImpl implements ICanvasController {
    @Autowired
    @Qualifier("canvasService")
    private CanvasServiceImpl canvasService;

    //转向添加油画页面
    @RequestMapping("/add")
    public String Promptadd() {
        return "back/add_canvas";
    }

    //转向后台显示油画页面
    @RequestMapping("/index")
    public String PromptClist() {
        return "back/canvas_list";
    }

    @RequestMapping("/listdo")
    //转向主页面
    public String Promptlistdo(HttpSession session) { return "pre/all_list"; }

    @RequestMapping("/detail")
    //转向详细页面
    public String Promptdetail(){
        return "pre/detail";
    }

    /**
     * 卖画咯卖画咯
     *
     * @return
     */
    @RequestMapping("/insertC")
    @ResponseBody
    @Override
    public Object setCanvas(@RequestBody Canvas canvas, HttpSession session) {
        /*从session中获取uid 并放入实体中 之后的发布人有关
        canvas.setUid();
        session.getAttribute();
        System.out.println(canvas.getImg());*/
        /*User user=new User();
        user= (User) session.getAttribute("user");
        if (user.getUid()==null||"".equals(user.getUid().trim())){
           return APIResult.createError(new APICodeConstant(),"no user please login");
        }
        canvas.setUid(user.getUid());*/
        System.out.println("price"+canvas.getPrice());
        String str=canvas.getImg();
        String temp[]=str.split("\\\\");
        canvas.setImg(temp[2]);//直接存图片*.jpg
        canvasService.addCanvas(canvas);
        return APIResult.createSuccessMessage(new APICodeConstant(), "insertC success");
    }

    /**
     * 我要把油画给管理员看
     * 展示油画
     * skip是从每页的第n+1个油画开始展示
     * size是每页的油画数
     *
     * @return canvas列表
     */
    @RequestMapping("Clist")
    @ResponseBody
    @Override
    public Object getCanvasBycgid(String cgid) {
        Integer Cgid = 1;
        Pattern pattern = Pattern.compile("[0-9]*");
        if (pattern.matcher(cgid).matches()) {
            Cgid = Integer.valueOf(cgid);
        }
        System.out.println("cgid" + Cgid);
        List<Canvas> canvas = canvasService.getCanvasBycgid(Cgid, 0, 10);
        System.out.println("size" + canvas.size());
        return APIResult.createSuccess(new APICodeConstant(), canvas);
    }

    /**
     * 不卖这该死的油画了
     * 从数据库删除油画
     *
     * @return
     */
    @RequestMapping("Cdelete")
    @ResponseBody
    @Override
    public Object delCanvas(Integer cid) {
        if (cid == null)
            return APIResult.createError(new APICodeConstant(), "del failed");
        System.out.println("cid" + cid);
        canvasService.deleteOneByid(cid);
        return APIResult.createSuccessMessage(new APICodeConstant(), "del success");
    }

    /**
     * @author hongfive
     * @date 2019/1/29
      * @param cid
     * @return 一个油画信息
     */

    @RequestMapping("Cone")
    @ResponseBody
    @Override
    public Object getOnecanvas(String cid) {
        System.out.println("cid"+cid);
        /*Pattern pattern = Pattern.compile("[0-9]*");
        if (pattern.matcher(cid).matches()) {
            return APIResult.createError(new APICodeConstant(), "cid wrong");
        }*/
        Integer Cid = Integer.valueOf(cid);
        Canvas canvas = new Canvas();
        canvas = canvasService.getOneByid(Cid);
        return APIResult.createSuccess(new APICodeConstant(), canvas);
    }


}




